package com.yy.bootdemo.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.yy.bootdemo.admin.entity.MyMenu;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MenuTree1 {
    /**
     * 构建结束的树
     */
    private static List<Menu> menuList ;

    /**
     * 构建树
     * @param menuId 树的根节点id  0：代表顶级节点
     * @param nodeList 要构建的数据
     * @return
     */
    public static List<Menu> tree3(Integer menuId, List<MyMenu> nodeList){
        menuList = new ArrayList<Menu>();
        Menu menu1=new Menu();
        menu1.setMenuId(menuId);
        getNode3(menu1,nodeList);
        return menuList;
    }

    /**
     * 使用递归构建树
     * @param menu1
     * @param nodeList
     */
    public static void  getNode3(Menu menu1, List<MyMenu> nodeList){
        Integer menuId =menu1.getMenuId();
        for (MyMenu layer : nodeList) {
            if(menuId.toString().equals(layer.getParentId().toString())){
                Integer menuId1=layer.getMenuId();
                Menu menu=new Menu();
                menu.setMenuId(menuId1);
                menu.setMenuName(layer.getMenuName());
                menu.setParentId(layer.getParentId());
                if (menuId.toString().equals("0")){
                    menuList.add(menu);
                }else {
                    menu1.getChildren().add(menu);
                }
                getNode3(menu,nodeList);
            }
        }

    }

    public static void main(String[] args) {
        String str ="[{\"params\":{},\"menuId\":1,\"parentId\":0,\"menuName\":\"工作空间\",\"icon\":\"el-icon-document\",\"type\":0,\"url\":\"table\",\"permission\":\"\",\"sort\":1,\"1Params\":{}},{\"params\":{},\"menuId\":2,\"parentId\":1,\"menuName\":\"控制后台\",\"icon\":\"el-icon-star-off\",\"type\":1,\"url\":\"home\",\"permission\":\"\",\"sort\":2,\"1Params\":{}},{\"params\":{},\"menuId\":3,\"parentId\":0,\"menuName\":\"系统管理\",\"icon\":\"el-icon-setting\",\"type\":0,\"url\":\"sys\",\"permission\":\"\",\"sort\":3,\"1Params\":{}},{\"params\":{},\"menuId\":4,\"parentId\":3,\"menuName\":\"用户管理\",\"icon\":\"el-icon-s-custom\",\"type\":1,\"url\":\"user\",\"permission\":\"user:list\",\"sort\":4,\"1Params\":{}},{\"params\":{},\"menuId\":5,\"parentId\":3,\"menuName\":\"角色管理\",\"icon\":\"el-icon-star-off\",\"type\":1,\"url\":\"role\",\"permission\":\"role:list\",\"sort\":5,\"1Params\":{}},{\"params\":{},\"menuId\":6,\"parentId\":3,\"menuName\":\"菜单管理\",\"icon\":\"el-icon-tickets\",\"type\":1,\"url\":\"menu\",\"permission\":\"menu:list\",\"sort\":6,\"1Params\":{}},{\"params\":{},\"menuId\":7,\"parentId\":0,\"menuName\":\"系统监控\",\"icon\":\"layui-icon layui-icon-console\",\"type\":0,\"url\":\"see\",\"permission\":\"\",\"sort\":7,\"1Params\":{}},{\"params\":{},\"menuId\":8,\"parentId\":7,\"menuName\":\"SQL监控\",\"icon\":\"layui-icon layui-icon-chart\",\"type\":1,\"url\":\"/druid/login\",\"permission\":\"\",\"sort\":8,\"1Params\":{}},{\"params\":{},\"menuId\":9,\"parentId\":7,\"menuName\":\"接口文档\",\"icon\":\"layui-icon layui-icon-chart\",\"type\":1,\"url\":\"/swagger-ui.html\",\"permission\":null,\"sort\":9,\"1Params\":{}},{\"params\":{},\"menuId\":10,\"parentId\":0,\"menuName\":\"错误页面\",\"icon\":\"layui-icon layui-icon-auz\",\"type\":0,\"url\":null,\"permission\":null,\"sort\":10,\"1Params\":{}},{\"params\":{},\"menuId\":11,\"parentId\":10,\"menuName\":\"403\",\"icon\":\"layui-icon layui-icon layui-icon-face-cry\",\"type\":1,\"url\":\"/api/403\",\"permission\":\"\",\"sort\":11,\"1Params\":{}},{\"params\":{},\"menuId\":12,\"parentId\":10,\"menuName\":\"404\",\"icon\":\"layui-icon layui-icon-face-cry\",\"type\":1,\"url\":\"/api/404\",\"permission\":null,\"sort\":12,\"1Params\":{}},{\"params\":{},\"menuId\":13,\"parentId\":10,\"menuName\":\"500\",\"icon\":\"layui-icon layui-icon layui-icon layui-icon layui-icon layui-icon layui-icon-face-cry\",\"type\":1,\"url\":\"/api/500\",\"permission\":\"\",\"sort\":13,\"1Params\":{}},{\"params\":{},\"menuId\":35,\"parentId\":7,\"menuName\":\"操作日志\",\"icon\":\"layui-icon-group\",\"type\":1,\"url\":\"/api/log/index\",\"permission\":\"log:list\",\"sort\":7,\"1Params\":{}},{\"params\":{},\"menuId\":36,\"parentId\":7,\"menuName\":\"异常日志\",\"icon\":\"layui-icon-face-cry\",\"type\":1,\"url\":\"/api/log/error/index\",\"permission\":\"errorLog:list\",\"sort\":7,\"1Params\":{}},{\"params\":{},\"menuId\":68,\"parentId\":3,\"menuName\":\"部门管理\",\"icon\":\"layui-icon layui-icon layui-icon layui-icon-group\",\"type\":1,\"url\":\"/api/dept/index\",\"permission\":\"dept:list\",\"sort\":7,\"1Params\":{}},{\"params\":{},\"menuId\":69,\"parentId\":3,\"menuName\":\"岗位管理\",\"icon\":\"layui-icon layui-icon layui-icon layui-icon-group\",\"type\":1,\"url\":\"/api/job/index\",\"permission\":\"job:list\",\"sort\":8,\"1Params\":{}},{\"params\":{},\"menuId\":70,\"parentId\":0,\"menuName\":\"系统工具\",\"icon\":\"layui-icon layui-icon layui-icon-app\",\"type\":0,\"url\":\"view\",\"permission\":\"\",\"sort\":8,\"1Params\":{}},{\"params\":{},\"menuId\":71,\"parentId\":70,\"menuName\":\"定时任务\",\"icon\":\"layui-icon layui-icon-play\",\"type\":1,\"url\":\"/api/quartz/index\",\"permission\":\"\",\"sort\":9,\"1Params\":{}},{\"params\":{},\"menuId\":81,\"parentId\":7,\"menuName\":\"在线用户\",\"icon\":\"layui-icon layui-icon layui-icon layui-icon-username\",\"type\":1,\"url\":\"/api/online/index\",\"permission\":\"\",\"sort\":7,\"1Params\":{}},{\"params\":{},\"menuId\":82,\"parentId\":70,\"menuName\":\"代码生成\",\"icon\":\"layui-icon layui-icon layui-icon-fonts-code\",\"type\":1,\"url\":\"/api/generator/index\",\"permission\":\"\",\"sort\":10,\"1Params\":{}},{\"params\":{},\"menuId\":85,\"parentId\":70,\"menuName\":\"表单构建\",\"icon\":\"layui-icon layui-icon-fire\",\"type\":1,\"url\":\"/api/form/build\",\"permission\":\"\",\"sort\":11,\"1Params\":{}},{\"params\":{},\"menuId\":86,\"parentId\":3,\"menuName\":\"字典管理\",\"icon\":\"layui-icon layui-icon-form\",\"type\":1,\"url\":\"location\",\"permission\":\"dict:list\",\"sort\":9,\"1Params\":{}},{\"params\":{},\"menuId\":90,\"parentId\":2,\"menuName\":\"32432\",\"icon\":\"324\",\"type\":1,\"url\":\"324\",\"permission\":\"324\",\"sort\":null,\"1Params\":{}},{\"params\":{},\"menuId\":91,\"parentId\":2,\"menuName\":\"32432\",\"icon\":\"324\",\"type\":1,\"url\":\"324\",\"permission\":\"324\",\"sort\":null,\"1Params\":{}},{\"params\":{},\"menuId\":95,\"parentId\":3,\"menuName\":\"图片管理\",\"icon\":\"el-icon-picture\",\"type\":1,\"url\":\"img\",\"permission\":\"img\",\"sort\":5,\"1Params\":{}},{\"params\":{},\"menuId\":97,\"parentId\":3,\"menuName\":\"图片管理\",\"icon\":\"el-icon-picture\",\"type\":1,\"url\":\"img\"," +
                "\"permission\":\"img\",\"sort\":5,\"1Params\":{}}]";

        List<MyMenu> list=jsonToList(str,MyMenu.class);
      //  System.out.println(list);
        System.out.println();
       // System.out.println(JSON.toJSONString(list));

        List<Menu> menuList1 =   tree3(0,list);
        System.out.println(JSON.toJSONString(menuList1));
    }

    public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) {
        return JSONArray.parseArray(jsonData, beanType);
    }

    public  List<Map<String,Object>> tree2(Integer pid, List<MyMenu> nodeList){
        List<Map<String,Object>> resultList =getNodeX(pid,nodeList);

        for (Map<String,Object> menu:resultList ){
            getNode2(menu,nodeList);
        }

        return resultList;
    }
    public  List<Map<String,Object>> getNodeX(Integer pid, List<MyMenu> nodeList){
        List<Map<String,Object>> resultList = new ArrayList<>();

        for (MyMenu layer : nodeList) {
            if(pid.equals(layer.getParentId())){
                Map<String,Object> menu=new HashMap<>();
                menu.put("value",layer.getMenuId());
                menu.put("label",layer.getMenuName());
                menu.put("parentId",layer.getParentId());
                resultList.add(menu);

            }
        }
        return resultList;
    }

    public  void  getNode2(Map<String,Object> menu2, List<MyMenu> nodeList){
        List<Map<String,Object>> resultList = new ArrayList<>();
        for (MyMenu layer : nodeList) {
            if(menu2.get("value").toString().equals(layer.getParentId().toString())){
                Map<String,Object> menu=new HashMap<>();
                menu.put("value",layer.getMenuId());
                menu.put("label",layer.getMenuName());
                menu.put("parentId",layer.getParentId());
                resultList.add(menu);
                getNode2(menu,nodeList);
            }
        }
        if (resultList.size()>0){

            menu2.put("children",resultList);
        }
    }

   /* public  List<Menu> tree(Integer pid, List<MyMenu> nodeList){
        List<Menu> resultList = new ArrayList<>();
        if (pid!=null&&null != nodeList && nodeList.size() > 0){
            Menu  menu1=new Menu();
            for (MyMenu layer : nodeList) {
               if(pid.equals(layer.getParentId())){
                    Menu menu=new Menu();
                    menu.setValue(layer.getMenuId());
                    menu.setParentId(layer.getParentId());
                    menu.setLabel(layer.getMenuName());
                    resultList.add(menu);

                }
            }
            for (MyMenu layer : nodeList) {
                if(pid.toString().equals(layer.getMenuId().toString())){
                    menu1.setValue(layer.getMenuId());
                    menu1.setParentId(layer.getParentId());
                }
            }
            menu1.setChildren(resultList);
            menuList.add(menu1);
            tree(menu1.getParentId(),nodeList);
        }
        return menuList;
    }*/

   /* public  List<Menu> tree1(Integer pid, List<MyMenu> nodeList){
        List<Menu> resultList =getNode(pid,nodeList);
        for (Menu menu:resultList ){
            Integer parentId=  menu.getParentId();
            getNode1(menu,nodeList);
        }

        return resultList;
    }
    public  void  getNode1(Menu menu2, List<MyMenu> nodeList){
        List<Menu> resultList = new ArrayList<>();
        for (MyMenu layer : nodeList) {
            if(menu2.getValue().toString().equals(layer.getParentId().toString())){
                Menu menu=new Menu();
                menu.setValue(layer.getMenuId());
                menu.setParentId(layer.getParentId());
                menu.setLabel(layer.getMenuName());
                resultList.add(menu);
                getNode1(menu,nodeList);
            }
        }
        menu2.setChildren(resultList);
    }
    public  List<Menu> getNode(Integer pid, List<MyMenu> nodeList){
        List<Menu> resultList = new ArrayList<>();
        Menu  menu1=new Menu();
        for (MyMenu layer : nodeList) {
            if(pid.equals(layer.getParentId())){
                Menu menu=new Menu();
                menu.setValue(layer.getMenuId());
                menu.setParentId(layer.getParentId());
                menu.setLabel(layer.getMenuName());
                resultList.add(menu);

            }
        }
        return resultList;
    }*/

}
